package com.canaan.business.controller.pc.inOutStock;

import com.canaan.business.common.enums.inOutStock.OutStockOrderTypeEnum;
import com.canaan.business.domain.inOutStock.WmsOutStockOrder;
import com.canaan.business.domain.vo.*;
import com.canaan.business.service.inOutStock.IWmsOutStockOrderService;
import com.canaan.common.annotation.Log;
import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.AjaxResult;
import com.canaan.common.core.page.TableDataInfo;
import com.canaan.common.enums.BusinessType;
import com.canaan.common.utils.bean.BeanUtils;
import com.canaan.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 出库单Controller
 *
 * @author ruoyi
 * @date 2022-12-20
 */
@RestController
@RequestMapping("/inOutStock/outStockOrder")
public class WmsOutStockOrderController extends BaseController
{
    @Resource
    private IWmsOutStockOrderService wmsOutStockOrderService;

    /**
     * 查询出库单列表
     */
    @ApiOperation(value = "查询出库单列表")
    @GetMapping("/list")
    public TableDataInfo list(WmsOutStockOrder outStockOrder) {
        startPage();
        List<WmsOutStockOrder> list = wmsOutStockOrderService.selectWmsOutStockOrderList(outStockOrder);
        return getDataTable(list);
    }

    /**
     * 导出出库单列表
     */
    @Log(title = "出库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsOutStockOrder wmsOutStockOrder)
    {
        List<WmsOutStockOrderVo> list = wmsOutStockOrderService.exportList(wmsOutStockOrder);
        if(OutStockOrderTypeEnum.SALES.getCode().equals(wmsOutStockOrder.getOrderType())){
            //销售出库
            ExcelUtil<WmsOutStockOrderExport1Vo> salesTypeUtil = new ExcelUtil<WmsOutStockOrderExport1Vo>(WmsOutStockOrderExport1Vo.class);
            List<WmsOutStockOrderExport1Vo> exportList = BeanUtils.copyList(list,WmsOutStockOrderExport1Vo.class);
            salesTypeUtil.exportExcel(response, exportList, OutStockOrderTypeEnum.SALES.getMessage() + "单数据");
        }else if(OutStockOrderTypeEnum.PICK.getCode().equals(wmsOutStockOrder.getOrderType())){
            //领料出库
            ExcelUtil<WmsOutStockOrderExport2Vo> pickTypeUtil = new ExcelUtil<WmsOutStockOrderExport2Vo>(WmsOutStockOrderExport2Vo.class);
            List<WmsOutStockOrderExport2Vo> exportList = BeanUtils.copyList(list,WmsOutStockOrderExport2Vo.class);
            pickTypeUtil.exportExcel(response, exportList, OutStockOrderTypeEnum.PICK.getCode() + "单数据");
        }else if(OutStockOrderTypeEnum.ADJUST.getCode().equals(wmsOutStockOrder.getOrderType())){
            //调拨出库
            ExcelUtil<WmsOutStockOrderExport3Vo> adjustTypeUtil = new ExcelUtil<WmsOutStockOrderExport3Vo>(WmsOutStockOrderExport3Vo.class);
            List<WmsOutStockOrderExport3Vo> exportList = BeanUtils.copyList(list,WmsOutStockOrderExport3Vo.class);
            adjustTypeUtil.exportExcel(response, exportList, OutStockOrderTypeEnum.ADJUST.getCode() + "单数据");
        }else if(OutStockOrderTypeEnum.OTHER.getCode().equals(wmsOutStockOrder.getOrderType())){
            //其他出库
            ExcelUtil<WmsOutStockOrderExport4Vo> otherTypeUtil = new ExcelUtil<WmsOutStockOrderExport4Vo>(WmsOutStockOrderExport4Vo.class);
            List<WmsOutStockOrderExport4Vo> exportList = BeanUtils.copyList(list,WmsOutStockOrderExport4Vo.class);
            otherTypeUtil.exportExcel(response, exportList, OutStockOrderTypeEnum.OTHER.getCode() + "单数据");
        }else if(OutStockOrderTypeEnum.RETURN.getCode().equals(wmsOutStockOrder.getOrderType())){
            //退货出库
            ExcelUtil<WmsOutStockOrderExport5Vo> otherTypeUtil = new ExcelUtil<WmsOutStockOrderExport5Vo>(WmsOutStockOrderExport5Vo.class);
            List<WmsOutStockOrderExport5Vo> exportList = BeanUtils.copyList(list,WmsOutStockOrderExport5Vo.class);
            otherTypeUtil.exportExcel(response, exportList, OutStockOrderTypeEnum.RETURN.getCode() + "单数据");
        }
    }

    /**
     * 获取出库单详细信息
     */
    @ApiOperation(value = "获取出库单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(wmsOutStockOrderService.selectWmsOutStockOrderById(id));
    }

    /**
     * 新增出库单
     */
    @ApiOperation(value = "新增出库单")
    @Log(title = "出库单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsOutStockOrder outStockOrder) {
        return toAjax(wmsOutStockOrderService.insertWmsOutStockOrder(outStockOrder));
    }

    /**
     * 修改出库单
     */
    @ApiOperation(value = "修改出库单")
    @Log(title = "出库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsOutStockOrder outStockOrder) {
        return toAjax(wmsOutStockOrderService.updateWmsOutStockOrder(outStockOrder));
    }

    /**
     * 出库单取消
     */
    @PutMapping("/cancel")
    @ApiOperation(value = "取消")
    @Log(title = "出库单", businessType = BusinessType.UPDATE)
    public AjaxResult cancel(@RequestBody WmsOutStockOrder outStockOrder) {
        return toAjax(wmsOutStockOrderService.cancel(outStockOrder));
    }

    /**
     * 删除出库单
     */
    @ApiOperation(value = "删除出库单")
    @Log(title = "出库单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(wmsOutStockOrderService.deleteWmsOutStockOrderByIds(ids));
    }
}
